package top.kaoshanji.r6ch13c;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/5/11 19:08
 */
@ContextConfiguration(locations = { "classpath:r6ch13c/application-context.xml" })
public class ForumDaoTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private ForumDao forumDao;

    // 代码清单test-r6ch13c-431页a：参数通过数组传递，每个参数对应SQL语句里的一个?
    @Test
    public void addForumV1() {
        Forum forum = new Forum();
        forum.setForumName("V1二手市场");
        forum.setForumDesc("V1二手货物的交流论坛。");
        forumDao.addForumV1(forum);
    }

    // 代码清单test-r6ch13c-432页a：显式指定每个占位符所对应的字段数据类型
    @Test
    public void addForumV2() {
        Forum forum = new Forum();
        forum.setForumName("V2二手市场");
        forum.setForumDesc("V2二手货物的交流论坛。");
        forumDao.addForumV2(forum);
    }

    // 代码清单test-r6ch13c-433页a：通过创建PreparedStatementSetter对象绑定参数的匿名内部类回调
    @Test
    public void addForumV3() {
        Forum forum = new Forum();
        forum.setForumName("V3二手市场");
        forum.setForumDesc("V3二手货物的交流论坛。");
        forumDao.addForumV3(forum);
    }

    // 代码清单test-r6ch13c-435页a：返回数据库的表自增主键值
    @Test
    public void addForumV4() {
        Forum forum = new Forum();
        forum.setForumName("V4二手市场");
        forum.setForumDesc("V4二手货物的交流论坛。");
        forumDao.addForumV4(forum);
    }

    // 代码清单test-r6ch13c-436页a：批量更改数据
    @Test
    public void addForumBatch() {
        List<Forum> forumList = new ArrayList<>();
        for(int i =0 ; i < 100 ;i++){
            Forum f1 = new Forum();
            f1.setForumName("爱美妈妈" + i);
            f1.setForumDesc("减肥、塑身、化妆品");
            forumList.add(f1);
        }
        forumDao.addForumBatch(forumList);
    }

    // 代码清单test-r6ch13c-437页a：使用RowCallbackHandler查询单条结果集
    @Test
    public void getForumV1() {
        Forum forum = forumDao.getForumV1(88815);
        System.out.println("========看看输出==========");
        System.out.println(forum);
    }

    // 代码清单test-r6ch13c-438页a：使用RowCallbackHandler查询多条结果集
    @Test
    public void getForumListV1() {
        List<Forum> forumList = forumDao.getForumListV1(88800 ,88818);
        System.out.println("========看看输出==========");
        System.out.println(forumList);
    }

    // 代码清单test-r6ch13c-438页b：使用RowCallbackHandler查询多条结果集
    @Test
    public void getForumListV2() {
        List<Forum> forumList = forumDao.getForumListV2(88800 ,88818);
        System.out.println("========看看输出==========");
        System.out.println(forumList);
    }

    // 代码清单test-r6ch13c-01：单列单行
    @Test
    public void getForumCount() {
        System.out.println("========看看输出==========");
        System.out.println(forumDao.getForumCount());
    }

}
